#include "login.h"
#include "ui_login.h"
#include<QJsonDocument>
#include<QJsonObject>
#include<QJsonArray>
login::login(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::login)
{
    ui->setupUi(this);
    setWindowFlag(Qt::FramelessWindowHint);
    init();
}

login::~login()
{
    delete ui;
}

void login::init()
{
    ea->hide();
    int addressnum1=QRandomGenerator::global()->generate()%255;
    int addressnum2=QRandomGenerator::global()->generate()%255;
    int addressnum3=QRandomGenerator::global()->generate()%255;
    address+="."+QString::number(addressnum1);
    address+="."+QString::number(addressnum2);
    address+="."+QString::number(addressnum3);
    defaultImage.load(":/res/login_default.png");
    dafaultimage_path=":/res/login_default.png";
    usedimage_path=":/res/login_default.png";
    tso=new QTcpSocket;
    connect(tso,&QTcpSocket::readyRead,this,[=](){
        QByteArray ba=tso->readAll();
        QJsonDocument jd=QJsonDocument::fromJson(ba);
        QJsonObject jo=jd.object();
        QString dt=jo.value("datatype").toString(); //分析数据类型
        qDebug()<<"login:"<<dt;
        if(dt=="sendusedimage") //获取之前使用过的图片
        {
            QString image_path=jo.value("usedimage").toString();
            UsedImage=QImage(image_path);
            usedimage_path=image_path;
            if(!UsedImage.isNull())
            {
                ui->label->setPixmap(QPixmap::fromImage(UsedImage));
            }

            else
            {
                ui->label->setPixmap(QPixmap::fromImage(defaultImage));
                usedimage_path=dafaultimage_path;
            }
        }
        else if(dt=="confirmaccountexist") //验证账号是否存在
        {
            accountexist=jo.value("exist").toBool();
            QString Nam=jo.value("name").toString();
            QString image=jo.value("image").toString();
            qDebug()<<Nam<<" "<<image;
            if(accountexist)  //账户存在的话
            {
                w=new widget;
                w->setGeometry(200,100,1000,800);
                QImage im(image);
                w->init(tso,ui->account->text(),Nam,im,image,"无");
                w->show();
                this->hide();
            }
            else
            {
                loginfault* lf=new loginfault;
                lf->show();
                ui->account->clear();
                ui->secret->clear();
            }
        }
        else if(dt=="enrollinfo") //获取账号注册信息(是否成功)
        {
            //修改enrollaccount类的变量enrollsuccess
            ea->enrollsuccess=jo.value("enrollsuccess").toBool();
            if(ea->enrollsuccess)  //注册成功时，创建新界面widget
            {
                w=new widget;
                w->setGeometry(200,100,1000,800);
                w->init(tso,account,name,defaultImage,dafaultimage_path,"无");
                w->show();
                this->hide();
                ea->close();
            }
        }
        else
        {
            w->loginjo=jo;
            w->newjo=true;
        }

    });
    tso->connectToHost(QHostAddress(address),8081);
}

void login::GainUsedImage()
{
    QString str=ui->account->text();
    QJsonObject jo;
    jo.insert("datatype","usedimage");
    jo.insert("account",str);
    QJsonDocument jd(jo);
    tso->write(jd.toJson());
}




void login::EnrollAccount()
{
    //创建注册页面

    connect(ea,&enrollaccount::setAccount,this,[=](QString str){
        account=str;
    });
    connect(ea,&enrollaccount::setSecret,this,[=](QString str){
        secret=str;
    });
    connect(ea,&enrollaccount::setName,this,[=](QString str){
        name=str;
    });
    connect(ea,&enrollaccount::infoready,this,[=](){
        //向服务端发送并注册账户信息
        if(!account.isEmpty()&&!secret.isEmpty()&&!name.isEmpty())
        {
            QJsonObject ja;
            //设置账号注册信息
            ja.insert("datatype","enrollaccount");
            ja.insert("account",account);
            ja.insert("name",name);
            ja.insert("secret",secret);
            ja.insert("image",dafaultimage_path);
            QJsonDocument jo(ja);
            tso->write(jo.toJson());
        }
    });
    ea->show();

}






void login::on_account_textChanged(const QString &arg1)
{
    if(ui->account->text().isEmpty())
        account_null=true;
    else
        account_null=false;
    if(!account_null&&!secret_null)
    {
        ui->pushButton->setEnabled(true);
        ui->pushButton->setStyleSheet("background-color:#0099ff;border-radius:30px;color:rgb(255, 255, 255)");
    }
    else
    {
        ui->pushButton->setEnabled(false);
        ui->pushButton->setStyleSheet("background-color:#9ad8ff;border-radius:30px;color:rgb(255, 255, 255)");
    }

    //账号文本改变要想服务端确认是否存在此用户
    GainUsedImage();
}


void login::on_secret_textChanged(const QString &arg1)
{
    if(ui->secret->text().isEmpty())
        secret_null=true;
    else
        secret_null=false;
    if(!account_null&&!secret_null)
    {
        ui->pushButton->setEnabled(true);
        ui->pushButton->setStyleSheet("background-color:#0099ff;border-radius:30px;color:rgb(255, 255, 255)");
    }
    else
    {
        ui->pushButton->setEnabled(false);
        ui->pushButton->setStyleSheet("background-color:#9ad8ff;border-radius:30px;color:rgb(255, 255, 255)");
    }

}


void login::on_pushButton_clicked()
{
    //给服务端发送自己的账号信息，让服务端验证账号是否存在
    QJsonObject jo;
    jo.insert("datatype","accountexist");
    jo.insert("account",ui->account->text());
    QJsonDocument jd(jo);
    tso->write(jd.toJson());
}


void login::on_pushButton_2_clicked()
{
    EnrollAccount();
}


void login::on_pushButton_3_clicked()
{
    this->hide();
}

